package com.ruangong.GitClient.HunHePanAPI;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class getNew{
    public static String getNewResource() {
        String apiUrl = "https://api.hunhepan.com/v1/raw_disk/latest_with_extab?page=1&size=15";
        StringBuilder response = new StringBuilder();
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                String inputLine;


                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

            } else {
                System.out.println("Error in API call, response code: " + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response.toString();
    }

    public static List<MovieLink> getResult() {
        List<MovieLink> movieLinks = new ArrayList<>();

        String apiResponse = getNew.getNewResource();
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(apiResponse, JsonObject.class);
        if (jsonObject == null) return null;
        JsonObject dataObject = jsonObject.getAsJsonObject("data");

        // 检查 dataObject 是否为 null
        if (dataObject != null) {
            JsonArray listArray = dataObject.getAsJsonArray("list");

            // 检查 li
            // stArray 是否为 null
            if (listArray != null) {
                // 遍历 listArray，获取 disk_type 和 link，并存入 MovieLink 对象列表
                for (JsonElement element : listArray) {
                    JsonObject item = element.getAsJsonObject();
                    String diskType = item.get("disk_type").getAsString();
                    //System.out.println(diskType);
                    String link = item.get("link").getAsString();
                    //System.out.println(link);
                    String diskName = item.get("disk_name").getAsString();
                    //System.out.println(diskName);
                    String shareUser = item.get("share_user").getAsString();
                    //System.out.println(shareUser);
                    // 判断 is_mine 是否为 false


                    MovieLink movieLink = new MovieLink(diskType, diskName, link, shareUser);
                    movieLinks.add(movieLink);


                }
            } else {
                // 处理 listArray 为 null 的情况
                return null;
            }
        } else {
            // 处理 dataObject 为 null 的情况
            return null;
        }
        return movieLinks;
    }
//    public static void main(String[] args) {
//        System.out.println(getNew.getNewResource());
//        List<MovieLink> movieLinks = getNew.getResult();
//        // 获取 movieLinks 列表
//
//        for (MovieLink link : movieLinks) {
//            System.out.println("Disk Type: " + link.getDiskType() + ", Link: " + link.getLink() + ", DiskName: " + link.getDiskName()+", DiskType " + link.getDiskType());
//        }
//    }
}

